package com.example.wdanzuobq;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.ContactsContract;

public class CRUD {
    SQLiteOpenHelper dbHandler;
    SQLiteDatabase db;

    private static final String[] columns ={
            NoteDatabase.ID,
            NoteDatabase.CONTENT,
            NoteDatabase.TIME,
            NoteDatabase.MODE
    };
    public CRUD(Context context){
        dbHandler = new NoteDatabase(context);
    }
    public void open(){
        db = dbHandler.getWritableDatabase();
    }
    public void close(){
        dbHandler.close();
    }
    public NOte addNote(NOte note){
        ContentValues contentValues = new ContentValues();
        contentValues.put(NoteDatabase.CONTENT,note.getContent());
        contentValues.put(NoteDatabase.TIME,note.getTime());
        contentValues.put(NoteDatabase.MODE,note.getTag());
        long insertId = db.insert(NoteDatabase.TABLE_NAME,null,contentValues);
        note.setId(insertId);
        return note;
    }
    public NOte getNote(long id){
        Cursor cursor = db.query(NoteDatabase.TABLE_NAME,columns,NoteDatabase.ID + "=?",
                new String[]{
                        String.valueOf(id)},null,null,null,null);
        if (cursor!= null) cursor.moveToFirst();
        NOte e = new NOte(cursor.getString(1),cursor.getString(2),cursor.getInt(3));
        return e;
    }



}
